package com.thti.mecool.client.model.biz;

import java.util.List;

import android.content.Context;
import android.database.Cursor;

import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.db.DataBase;
import com.thti.mecool.client.model.po.Project;
import com.thti.mecool.client.model.po.SalersProject;
import com.thti.mecool.client.model.po.Task;

public class SalersProjectBizImpl {
	
	private DataBase dataBase;

	public SalersProjectBizImpl(Context context) {
		dataBase = new DataBase(context);
	}
	public void save(SalersProject salersProject){
		String sqlString="insert into saleproject(userId,projectId,name,storeId,updateTime,isType) values(?,?,?,?,?,?)";
		dataBase.execObject(sqlString, new Object[]{salersProject.getUserId(),salersProject.getProjectId(),salersProject.getName()
				,salersProject.getStoreId(),salersProject.getUpdateTime(),salersProject.getIsType()});
		dataBase.close();
	}
	
	
	public void update(SalersProject salersProject){
		String sqlString="update project set name=? where userId=? and projectId=?";
		dataBase.execObject(sqlString, new String[]{salersProject.getName(),salersProject.getUserId(),salersProject.getProjectId()});
		dataBase.close();
	}
	
	/**
	 * 判断projrctId是已经存在
	 * @param id
	 * @return
	 */
	public boolean findProjectId(String userId,String projectId){
		String sqlString="select  * from saleproject where userId=? and projectId=?";
		Cursor cursor=dataBase.queryObject(sqlString, new String[]{userId,projectId});
		
		if(cursor.moveToNext()){
			boolean flag = cursor.getLong(0)>0 ?true:false;
			cursor.close();
			dataBase.close();
			return flag;
		}
		cursor.close();
		dataBase.close();
		return false;

	} 
		
	@SuppressWarnings("unchecked")
	public List<SalersProject> findAll(String userId){
		// select pro.planId,p.planName,p.userId,pro.projectName,t.taskId,t.taskName,t.storeId,t.storeName,t.startTime,t.endTime from plan p,project pro,task t where p.id=pro.planId AND pro.id=t.projectId
		String sql="select  * from  saleproject  where userId=?";
		List<SalersProject> list=dataBase.findAll(SalersProject.class, sql, new String[]{userId});
		dataBase.close();
		return list;
		
	}

}
